package com.service.calculate.vehicle;

import java.text.SimpleDateFormat;
import java.util.concurrent.PriorityBlockingQueue;
import com.service.bean.device.Vehicle;
import com.service.bean.garbage.SendTrackData;
import com.service.bean.other.VariableData;
import com.service.bean.signal.GpsSignal;
import com.service.bean.signal.GpsSignalWork;
import com.service.bean.alarm.AlarmInfo;
import com.service.bean.alarm.AlarmRule;
import com.service.cache.CacheManager;
import com.service.garbage.ningbo.GarbageAddress;
import com.service.other.Logger;
import com.service.tools.NoticeMessageTool;

public class ScalarCal implements Runnable {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	public void run() {
		try {
			PriorityBlockingQueue<GpsSignal> signals = CacheManager.instance().getSignalQueue(Thread.currentThread().getName());
			GpsSignalWorkCal workCal = null;
			DutyReportCal reportCal = null;
			while ((signals.peek()) != null) {
				GpsSignal signal = signals.poll();
				Vehicle vehicle = CacheManager.instance().getVehicle(signal.gpsNo);
				new SignalCreator(signal,vehicle).createSignalInScalarCal();
				CacheManager.instance().getGpsSaveQueue(vehicle.company).offer(signal);
				// 出勤报表统计
				reportCal = new DutyReportCal(vehicle, signal);
				reportCal.attendanceCal();
				// 计算报警
				calculateAlarm(vehicle, signal);
				
				// 根据当前的GpsSignal对象计算变化量
				workCal = new GpsSignalWorkCal(signal, vehicle);
				GpsSignalWork work = workCal.cal();
				if (null != work) {
					CacheManager.instance().getWorkSaveQueue(vehicle.company).offer(work);
				}
				
				// 计算油量
				if(signal.speed <= 5){
					vehicle.oilBean.setRealOilCal(signal,vehicle);
				}
				
				// 根据当前GpsSignal对象的速度计算速度变量
//				if (signal.speed > 0) {
//					SpeedCalculator speed = new SpeedCalculator(signal);
//					List<GpsSignalWorkSpeed> list = speed.speedCalculator();
//					if (list!=null && list.size()!=0) {
//						CacheManager.instance().getWorkSpeedSaveQueue(vehicle.company).addAll(list);
//					}
//				}
				//刷新每个gpsno对应的最新Signal
				if (work != null) {
					signal.oilChange = work.oilChange;
					signal.mileChange = work.mileChange;
					signal.sumStopTime = work.sumStopTime;
					signal.sumWorkStopTime = work.sumWorkStopTime;
				}
				
				// 宁波新世纪接口调用 TODO
				if ("032".equals(vehicle.company.code)) {
					String sendMessage = new SendTrackData(vehicle, signal).toString();
					GarbageAddress ga = new GarbageAddress(vehicle, null, null, sendMessage);
					ga.sendTrackData();
				}
				
				// 放入缓存管道，每10分钟进行一次补点 
				CacheManager.instance().getSignalForTelematicsQueue(signal.gpsNo).add(signal);
				// 车辆Gps信号进入后，清除缓存中的离线时间
				VariableData variable = CacheManager.instance().getVariable(signal.gpsNo);
				variable.offlineTime = null;
				
				vehicle.lastGps = signal;
				
			}
			Thread.currentThread().interrupt();
		} catch (Exception e) {
			Logger.err(e);
		}
		
	}
	
	/**
	 * 车辆报警
	 */
	private void calculateAlarm(Vehicle vehicle, GpsSignal signal){
		AlarmInfo ai = null;
		for(AlarmRule rule : vehicle.alarmRuleMap.values()){
			try {
				ai = rule.doAlarm(vehicle, signal);
				if (null != ai) {
					CacheManager.instance().getAlarmSaveQueue(vehicle.company).offer(ai);
					NoticeMessageTool.checkSendMessag(ai.signalId,ai.gpsNo, ai.alarmSubType, signal, ai);	
				}
			} catch (Exception e) {
				Logger.err(e);
			}
		}
	}
	
}
